define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/project/index' + location.search,
                    add_url: 'project/project/add',
                    edit_url: 'project/project/edit',
                    del_url: 'project/project/del',
                    table: 'project',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true,},
                        { field: 'id', title: __('Id'), sortable: false },
                        { field: 'title', title: __('title'), operate: 'LIKE' },
                        { field: 'price', title: __('price') , operate: false},
                        { field: 'nums_text', title: __('nums') , operate: false},
                        { field: 'thumbs', title: __('thumbs'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'status', title: __('status'), searchList: { "0": __('未开始'), "1": __('开始报名'), "2": __('截止报名'), "3": __('活动已开始'), "4": __('活动已结束') }, formatter: Table.api.formatter.status },
                        { field: 'createtime', title: __('createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'endtime', title: __('endtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                // 开始报名
                                {
                                    name: 'start', text: '开始报名',
                                    title: '开始报名',
                                    classname: 'btn btn-warning btn-xs btn-ajax',
                                    icon: 'fa fa-toggle-on',
                                    url: 'project/project/signtoggle',
                                    confirm: '确认开始报名吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (err) {
                                        console.log(err);
                                    },
                                    visible: function (row) {
                                        // console.log(row);
                                        return row.status == 0 ? true : false
                                    }
                                },
                                // 截止报名
                                {
                                    name: 'stop', text: '截至报名',
                                    title: '截至报名',
                                    classname: 'btn btn-success btn-xs btn-ajax',
                                    icon: 'fa fa-power-off',
                                    url: 'project/project/signtoggle',
                                    confirm: '确认截至报名吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (err) {
                                        console.log(err);
                                    },
                                    visible: function (row) {
                                        // console.log(row);
                                        return row.status == 1 ? true : false
                                    }
                                },
                                // 开始活动
                                {
                                    name: 'open', text: '开始活动',
                                    title: '开始活动',
                                    classname: 'btn btn-warning btn-xs btn-ajax',
                                    icon: 'fa fa-toggle-on',
                                    url: 'project/project/ActionToggle',
                                    confirm: '确认开始活动吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (err) {
                                        console.log(err);
                                    },
                                    visible: function (row) {
                                        // console.log(row);
                                        return row.status == 2 ? true : false
                                    }
                                },
                                // 结束活动
                                {
                                    name: 'off', text: '结束活动',
                                    title: '结束活动',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-power-off',
                                    // fa fa-toggle-off
                                    url: 'project/project/ActionToggle',
                                    confirm: '确认结束活动吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (err) {
                                        console.log(err);
                                    },
                                    visible: function (row) {
                                        // console.log(row);
                                        return row.status == 3 ? true : false
                                    }
                                },
                                // 查看订单
                                {
                                    name: 'order', text: '查看订单',
                                    title: '查看订单',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-bars',
                                    url: 'project/project/order',
                                    extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                },
                            ]
                        }
                    ]
                ],
                escape: false,
                // search:false,
            });
            
            $(".btn-editone").data("area", ["100%", "100%"])

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        order:function (){
            // console.log(Fast.api.query('ids'));
            Table.api.init({
                extend: {
                    index_url: 'project/project/order?ids=' + Fast.api.query('ids'),
                    table: 'project_order',
                }
            });
            // console.log(business.id);
            var orderTable = $("#table");
            orderTable.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'order.id',
                search: false,
                // 关闭导出
                showExport: false,
                // 关闭显示的列
                showColumns: false,
                // 关闭切换显示
                showToggle: false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { field: 'id', title: __('Id'), searchable: true },
                        { field: 'business.nickname', title: __('busid'), operate: 'LIKE' },
                        { field: 'total', title: __('total'), operate: 'LIKE' },
                        { field: 'status', title: __('Ostatus'), searchList: { "0": __('未开始'), "1": __('已开始'), "2": __('已结束')}, formatter: Table.api.formatter.status },
                        { field: 'createtime', title: __('Ocreatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },                    
                        { field: 'comment', title: __('comment'), operate: 'LIKE' },
                    ]
                ],
                escape: false,
            });
           
            // 为表格绑定事件
            Table.api.bindevent(orderTable);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
    };
    return Controller;
});